package com.mlz.mlzmall.ui.activity.frag.base;


import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.inter.NetResponses;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.util.ACache;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述：所有fragment都继承BaseFragment
 * Created by benny on 2016/5/7.
 * ProjectName：mlzMall.
 */
public class BaseFragment extends Fragment implements View.OnClickListener {
    protected String tag;
    protected String sessionId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sessionId = ACache.get(getActivity().getApplication()).getAsString("sessionKeyLogin");

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onClick(View v) {
        //监听事件这里处理

    }

    protected void showToast(String str) {
        //这里打印toast信息
        Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }

    protected String isNull(String str) {
        //这里判断值是否为空
        if (str == null) {
            return "";
        }
        return str;
    }
    public void request(int method, final String URL, Map map, final String session, String tag, final int type) {

        this.tag = tag;
        JSONObject jsonObject;
        if (map!=null)
            jsonObject = new JSONObject(map);
        else {
            jsonObject = null;
        }
        JsonRequest jsonRequest = new JsonObjectRequest(method, URL, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                netResponses.success(type, jsonObject);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                netResponses.fail();
            }
        }) {
            @Override
            public Map getHeaders() {
                HashMap headers = new HashMap();
                headers.put("Accept", "application/json");
                headers.put("Content-Type", "application/json; charset=UTF-8");
                if (!"".equals(session))

                    headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + session);

                return headers;
            }
        };
        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication)(getActivity().getApplication())).getRequestQueue().add(jsonRequest);
    }



    private NetResponses netResponses;

    public void setNetResponses(NetResponses netResponses) {
        this.netResponses = netResponses;
    }
}
